#include "delay.h"
//仿原子延时，不进入systic中断
void delay_us(uint32_t nus)
{

    uint32_t temp;
    SysTick->CTRL = 0x00; //关闭计数器
    SysTick->VAL = 0X00;  //清空计数器
    SysTick->LOAD = nus * 48 - 1;
    SysTick->VAL = 0X00; //清空计数器
    SysTick->CTRL |= SysTick_CLKSource_HCLK;
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
    do
    {
        temp = SysTick->CTRL;                         //读取当前倒计数值
    } while ((temp & 0x01) && (!(temp & (1 << 16)))); //等待时间到达
    SysTick->CTRL = 0x00;                             //关闭计数器
    SysTick->VAL = 0X00;                              //清空计数器
}
void delay_1ms(void)
{
    uint32_t temp;
    SysTick->CTRL = 0x00; //关闭计数器
    SysTick->VAL = 0X00;  //清空计数器
    SysTick->LOAD = 48000UL - 1;
    SysTick->VAL = 0X00; //清空计数器
    SysTick->CTRL |= SysTick_CLKSource_HCLK;
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
    do
    {
        temp = SysTick->CTRL;                         //读取当前倒计数值
    } while ((temp & 0x01) && (!(temp & (1 << 16)))); //等待时间到达
    SysTick->CTRL = 0x00;                             //关闭计数器
    SysTick->VAL = 0X00;                              //清空计数器
}

void delay_ms(uint16_t nms)
{
    while (nms--)
    {
        delay_1ms();
    }
}
